<?php
//通讯类
require_once(APPPATH . 'third_party/GatewayWorker/GatewayClient/Gateway.php');

use GatewayClient\Gateway;  // GatewayClient 3.0.0版本开始要使用命名空间
class GetwayWorker
{
    public function __construct()
    {
        // 设置GatewayWorker服务的Register服务ip和端口，请根据实际情况改成实际值(ip不能是0.0.0.0)
        Gateway::$registerAddress = '127.0.0.1:1238';
    }

    /*
     * 将客户端id与用户id绑定
     */
    public function bindUid($uid,$client_id,$group_id = '')
    {
        // client_id与uid绑定
        $res = Gateway::bindUid($client_id, $uid);

        if (!empty($group_id))
        {
            // 加入某个群组（可调用多次加入多个群组）
            Gateway::joinGroup($client_id, $group_id);
        }

        return $res;
    }

    //向用户id发送消息
    public function sendMessageToUid($uid,$message,$group = '')
    {
        // 向任意uid的网站页面发送数据
        Gateway::sendToUid($uid, $message);

        if (!empty($group))
        {
            // 向任意群组的网站页面发送数据
            Gateway::sendToGroup($group, $message);
        }
    }

    //向客户端id发送消息
    public function sendMessageToClient($uid,$message)
    {
        Gateway::sendToClient($uid, $message);
    }

    //向组发送消息
    public function sendToGroup($group, $message, $exclude_client_id = null)
    {
        Gateway::sendToGroup($group, $message, $exclude_client_id);
    }

    //向所有在线客户端发送消息
    public function sendToAll($message, $exclude_client_id = null,$client_id_array = null)
    {
        Gateway::sendToAll($message, $client_id_array, $exclude_client_id);
    }

    //向所有在线用户uid发送
    public function sendToUid($uid, $message)
    {
        Gateway::sendToUid($uid, $message);
    }
    
    //获取群组在线uid列表--在用
    public function getUidListByGroup($group)
    {
        return  Gateway::getUidListByGroup($group);
    }

    //获取当前在线数
    public function getAllClientIdCount()
    {
        return  Gateway::getAllClientIdCount();
    }

    //获取在线uid列表
    public function getAllUidList()
    {
        return  Gateway::getAllUidList();
    }


    //根据设备id获取uid
    public function getUidByClientId($client_id)
    {
        $c_id = Gateway::getUidByClientId($client_id);

        return $c_id;
    }

    //根据uid获取设备id列表
    public function getClientIdByUid($uid)
    {
        $ids = Gateway::getClientIdByUid($uid);

        return $ids;
    }

}